import axios from 'axios'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import { ElMessage } from "element-plus";

let requests = axios.create({
  //超时的设置
  timeout: 60 * 1000,
})

requests.interceptors.request.use((config) => {
  nprogress.start()
  return config
})

//响应拦截器：请求数据返回会执行
requests.interceptors.response.use(
  (res) => {
    if (res.request.responseType == 'blob') {
      const data = {
        data: res.data,
        fileName: res.headers['content-disposition'],
      }
      nprogress.done()
      return data
    } else {
      nprogress.done()
      return res.data
    }
  },
  (err) => {
    ElMessage({ message: err.message, type: "error" });
    return new Promise()
  }
)

export default requests
